﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyThietBi.BLL;
using QuanLyThietBi.DAL.Models;

namespace QuanLyThietBi
{
    public partial class ThongKe : Form
    {
        private ThietBiBO tbBO;
        private NhanVienBO nvBO;
        List<THIETBI> listThietBi = new List<THIETBI>();
        private const int pagesize = 20;
        private int currentPage = 1;
        private int totalPage = 1;

        public ThongKe()
        {
            InitializeComponent();
        }

        private void ThongKe_Load(object sender, EventArgs e)
        {
            BindDataToDataGridview();
            this.dtgThongKe.Columns["Hãng Sản Xuất"].Visible = false;
            this.dtgThongKe.Columns["Thiết bị ID"].Visible = false;
            BindDataToComboBoxPhongBan();
        }

        private void BindDataToDataGridview()
        {
            tbBO = new ThietBiBO();
            listThietBi = tbBO.GetAllThietBi();

            BindDataWithPagging(listThietBi);
        }

        private void BindDataWithPagging(List<THIETBI> listThietBi)
        {
            int totalItems = listThietBi.Count();

            totalPage = (totalItems + pagesize - 1) / pagesize;

            lblTrangHienTai.Text = "Trang " + currentPage + "/" + totalPage;

            lblTongThietBi.Text = totalItems + "";

            if (listThietBi != null && listThietBi.Count > 0)
            {
                DataTable table = new DataTable();
                table.Columns.Add("Thiết bị ID", typeof(int));
                table.Columns.Add("STT", typeof(int));
                table.Columns.Add("Tên Thiết Bị", typeof(string));
                table.Columns.Add("Mã Tài Sản", typeof(string));
                table.Columns.Add("Loại Thiết Bị", typeof(string));
                table.Columns.Add("Hãng Sản Xuất", typeof(string));
                table.Columns.Add("Tình Trạng", typeof(string));
                table.Columns.Add("Năm mua", typeof(string));
                table.Columns.Add("Nhân Viên", typeof(string));
                table.Columns.Add("Phòng Ban Đội", typeof(string));

                int startItem = currentPage * pagesize - pagesize;
                int endItem = currentPage != totalPage ? currentPage * pagesize - 1 : totalItems - 1;

                for (int i = startItem; i <= endItem; i++)
                {
                    table.Rows.Add(
                        listThietBi[i].id,
                        i+1,
                        listThietBi[i].ten_thiet_bi,
                        listThietBi[i].ma_thiet_bi,
                        GetLoaiThietBiByID(listThietBi[i].loai_thiet_bi),
                        listThietBi[i].hang_san_xuat,
                        GetTinhTrangById(listThietBi[i].tinh_trang),
                        listThietBi[i].nam_mua,
                        GetNhanVien(listThietBi[i].tinh_trang, listThietBi[i].id),
                        GetPhongBan(listThietBi[i].tinh_trang, listThietBi[i].id));
                }

                this.dtgThongKe.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dtgThongKe.DataSource = table;

                if (currentPage == 1)
                {
                    btnTrangDau.Enabled = false;
                    btnTrangTruoc.Enabled = false;
                }
                else
                {
                    btnTrangDau.Enabled = true;
                    btnTrangTruoc.Enabled = true;
                }

                if (currentPage == totalPage)
                {
                    btnTrangCuoi.Enabled = false;
                    btnTrangTiep.Enabled = false;
                }
                else
                {
                    btnTrangCuoi.Enabled = true;
                    btnTrangTiep.Enabled = true;
                }
            }
        }

        private string GetNhanVien(int idtinhtrang, int idthietbi)
        {
            string tennhanvien = string.Empty;
            if (idtinhtrang == 2)
            {
                nvBO = new NhanVienBO();
                tennhanvien = nvBO.GetTenNhanVien(idthietbi);
            }
            return tennhanvien;
        }

        private string GetPhongBan(int idtinhtrang, int idthietbi)
        {
            string tenphongban = string.Empty;
            PhongBanBO pbBO = new PhongBanBO();
            if (idtinhtrang == 2)
            {
                nvBO = new NhanVienBO();
                NHANVIEN nv = nvBO.GetNhanVien(idthietbi);
                if (nv != null)
                {
                    tenphongban = pbBO.GetTenPhongBan(nv.id);
                }
            }
            return tenphongban;
        }

        private string GetLoaiThietBiByID(int id)
        {
            LoaiThietBiBO ltbBO = new LoaiThietBiBO();
            return ltbBO.GetLoaiThietBiById(id);
        }

        private string GetTinhTrangById(int id)
        {
            TinhTrangBO ttBO = new TinhTrangBO();
            return ttBO.GetTinhTrangById(id);
        }

        private void btnTroLai_Click(object sender, EventArgs e)
        {
            Main frmmain = new Main();
            frmmain.Show();
            this.Hide();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            if (string.IsNullOrEmpty(tbxTenThietBi.Text.Trim()) && string.IsNullOrEmpty(tbxMaThietBi.Text.Trim()))
            {
                tbBO = new ThietBiBO();
                listThietBi = tbBO.SearchThietBi(string.Empty, string.Empty);
            }
            else
            {
                tbBO = new ThietBiBO();
                listThietBi = tbBO.SearchThietBi(tbxTenThietBi.Text.Trim(), tbxMaThietBi.Text.Trim());
            }
            BindDataWithPagging(listThietBi);
        }

        private void btnLichSu_Click(object sender, EventArgs e)
        {
            {
                int id = -1;
                foreach (DataGridViewRow row in this.dtgThongKe.SelectedRows)
                {
                    DataRow dc = (row.DataBoundItem as DataRowView).Row;
                    if (dc != null)
                    {
                        id = Convert.ToInt32(dc.ItemArray[0].ToString());
                    }
                    if (id != -1)
                    {
                        LichSuThietBi lstb = new LichSuThietBi(id);
                        lstb.Show();
                        this.Hide();
                    }
                }
            }
        }

        private void btnTrangTiep_Click(object sender, EventArgs e)
        {
            currentPage++;
            BindDataWithPagging(listThietBi);
        }

        private void btnTrangTruoc_Click(object sender, EventArgs e)
        {
            currentPage--;
            BindDataWithPagging(listThietBi);
        }

        private void btnTrangCuoi_Click(object sender, EventArgs e)
        {
            currentPage = totalPage;
            BindDataWithPagging(listThietBi);
        }

        private void btnTrangDau_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            BindDataWithPagging(listThietBi);
        }

        private void BindDataToComboBoxPhongBan()
        {
            PhongBanBO phongbanBO = new PhongBanBO();
            List<PHONGBAN> dsPhongBan = phongbanBO.GetAllPhongBan();
            if (dsPhongBan != null && dsPhongBan.Count > 0)
            {
                cbxPhongBan.DataSource = dsPhongBan;
                cbxPhongBan.DisplayMember = "phong_ban";
                cbxPhongBan.ValueMember = "id";
            }
        }
    }
}